﻿using System;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /*
     * This class is the base class for all the forms of geosketch.
     * This is mainly to handle the availability of the items of the main form (menu, buttons).
     * This class should be abstract, do not instantiate directly. It cannot be declared as abstract otherwise
     * the designer of the child form will crash.
     */

    public partial class GeoSketchForm : Form
    {
        #region Fields
        /// <summary>
        /// The parent status bar if interaction is needed
        /// </summary>
        protected StatusStrip ParentStatusBar;

        /// <summary>
        /// The parent toolbar if interaction is needed
        /// </summary>
        protected ToolStrip ParentToolbar; 
        #endregion

        #region Constructor
        /// <summary>
        /// !!! Never call this constructor, just for the designer window
        /// </summary>
        public GeoSketchForm()
            : this(new ActiveMDIParentItems(true), new GeoSketchMainForm())
        {
        }

        /// <summary>
        /// Call this constructor at all time
        /// </summary>
        public GeoSketchForm(ActiveMDIParentItems ActiveItems, Form MDIParentForm)
        {
            this.ActiveParentItems = ActiveItems;
            this.MdiParent = MDIParentForm;
            this.ParentStatusBar = ((GeoSketchMainForm)MDIParentForm).ParentStatusStrip;
            this.ParentToolbar = ((GeoSketchMainForm)MDIParentForm).ParentToolbar;
            SetActiveMDIParentItems();
        } 
        #endregion

        #region ItemsActions
        /// <summary>
        /// Contains the parent items to be enabled
        /// </summary>
        public ActiveMDIParentItems ActiveParentItems;

        /// <summary>
        /// The method when clicking the new button
        /// </summary>
        public virtual void MDIParentNew()
        {
        }

        /// <summary>
        /// The method when clicking the open button
        /// </summary>
        public virtual void MDIParentOpen()
        {
        }

        /// <summary>
        /// The method when clicking the save button
        /// </summary>
        public virtual void MDIParentSave()
        {
        }

        /// <summary>
        /// The method when clicking the save as button
        /// </summary>
        public virtual void MDIParentSaveAs()
        {
        }

        /// <summary>
        /// The method when clicking the print button
        /// </summary>
        public virtual void MDIParentPrint()
        {
        }

        /// <summary>
        /// The method when clicking the print preview button
        /// </summary>
        public virtual void MDIParentPrintPreview()
        {
        }

        /// <summary>
        /// The method when clicking the help button
        /// </summary>
        public virtual void MDIParentHelp()
        {
        } 
        #endregion

        #region Methods
        /// <summary>
        /// Sets the active parent items from the ActiveMDIParentItems object
        /// </summary>
        private void SetActiveMDIParentItems()
        {
            ((GeoSketchMainForm)(this.MdiParent)).SetActiveItems(ActiveParentItems);
        }

        /// <summary>
        /// Setups the form, auto-find the displaybox in the form, and affects it the viewport, and setup the toolbars
        /// </summary>
        protected void SetupForm(ToolStripMenuItem ToolbarsListMenuItem)
        {
            BaseGeoDisplayBox DisplayBox = new BaseGeoDisplayBox();
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(BaseGeoDisplayBox) || c.GetType().BaseType == typeof(BaseGeoDisplayBox))
                {
                    DisplayBox = (BaseGeoDisplayBox)c;
                }
            }
            ToolbarsListMenuItem.DropDownItems.Clear();
            SetupToolbars(DisplayBox,this.Controls,ToolbarsListMenuItem);
        }

        //Sets the displaybox to the toolbars, so they knew on wich displaybox they have to act on
        private void SetupToolbars(BaseGeoDisplayBox DisplayBox, Control.ControlCollection Controls,ToolStripMenuItem ToolbarsListMenuItem)
        {
            foreach (Control toolbar in Controls)
            {
                if (toolbar.GetType().BaseType == typeof(BaseToolbar))
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(((BaseToolbar)toolbar).Title);
                    item.Checked = true;
                    item.Tag = toolbar;
                    item.Click +=new EventHandler(ToggleToolbars);
                    ((BaseToolbar)toolbar).DisplayBox = DisplayBox;
                    ((BaseToolbar)toolbar).Parent = this;
                    ((BaseToolbar)toolbar).Tag = item;
                    ((BaseToolbar)toolbar).BringToFront();
                    ToolbarsListMenuItem.DropDownItems.Add(item);
                }
                else
                {
                    SetupToolbars(DisplayBox, toolbar.Controls,ToolbarsListMenuItem);
                }
            }
        }

        void ToggleToolbars(object sender, EventArgs e)
        {
            ((ToolStripMenuItem)sender).Checked = !((ToolStripMenuItem)sender).Checked;
            ((BaseToolbar)((ToolStripMenuItem)sender).Tag).Visible = ((ToolStripMenuItem)sender).Checked;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the dropdown button on the status bar corresponding to the left clic tool 
        /// </summary>
        public virtual ToolStripDropDownItem StatusStripLeftClicToolDropDownButton
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the textbox on the status bar corresponding to the left clic tool 
        /// </summary>
        public virtual ToolStripTextBox StatusStripLeftClicToolTextBox
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the dropdown button on the status bar corresponding to the right clic tool 
        /// </summary>
        public virtual ToolStripDropDownItem StatusStripRightClicToolDropDownButton
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the textbox on the status bar corresponding to the right clic tool 
        /// </summary>
        public virtual ToolStripTextBox StatusStripRightClicToolTextBox
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the label on the status bar to display messages
        /// </summary>
        public virtual ToolStripLabel StatusStripLabel
        {
            get
            {
                return null;
            }
        }
        #endregion
    }
}
